package com.tensquare.user.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import com.tensquare.util.IdWorker;
import com.tensquare.util.NotNullHelps;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;


import com.tensquare.user.dao.AdminDao;
import com.tensquare.user.model.Admin;

@Slf4j
@Service
public class AdminService {

	@Autowired
	private BCryptPasswordEncoder encoder ;

	@Autowired
	private AdminDao adminDao;
	
	@Autowired
	private IdWorker idWorker;

	private String message ;

	public void deleteById(String id) {
		adminDao.deleteById(id);
	}

	public void update(Admin admin) {
		adminDao.save(admin);
	}

	public void saveAdmin(Admin admin) {
		admin.setId(idWorker.nextId().toString());
		admin.setPassword(encoder.encode(admin.getPassword()));
		adminDao.save(admin);
	}

	public Admin loginAdmin(Admin admin) {
		Admin adminData = adminDao.findAdminsByLoginname(admin.getLoginname());
		if (NotNullHelps.isNotNull(adminData) == false) {
			message = "## 无此用户 @@";
		} else {
			String dataPassword = adminData.getPassword();
			if (encoder.matches(admin.getPassword(),dataPassword) == false) {
				message = "密码不对";
				adminData = null;
			} else {
				message = "$$密码验证无误@@";
			}
		}
		log.info(message);
		return adminData ;
	}
	private Specification<Admin> createSpecification(Map searchMap) {
		return new Specification<Admin>() {
			@Override
			public Predicate toPredicate(Root<Admin> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				List<Predicate> predicateList = new ArrayList<Predicate>();
				// ID
				if (searchMap.get("id")!=null && !"".equals(searchMap.get("id"))) {
					predicateList.add(cb.like(root.get("id").as(String.class), "%"+(String)searchMap.get("id")+"%"));
				}
				// 登陆名称
				if (searchMap.get("loginname")!=null && !"".equals(searchMap.get("loginname"))) {
					predicateList.add(cb.like(root.get("loginname").as(String.class), "%"+(String)searchMap.get("loginname")+"%"));
				}
				// 密码
				if (searchMap.get("password")!=null && !"".equals(searchMap.get("password"))) {
					predicateList.add(cb.like(root.get("password").as(String.class), "%"+(String)searchMap.get("password")+"%"));
				}
				// 状态
				if (searchMap.get("state")!=null && !"".equals(searchMap.get("state"))) {
					predicateList.add(cb.like(root.get("state").as(String.class), "%"+(String)searchMap.get("state")+"%"));
				}

				return cb.and( predicateList.toArray(new Predicate[predicateList.size()]));

			}
		};

	}

	public Page<Admin> findSearch(Map whereMap, int page, int size) {
		Specification<Admin> specification = createSpecification(whereMap);
		PageRequest pageRequest =  PageRequest.of(page-1, size);
		return adminDao.findAll(specification, pageRequest);
	}

	public List<Admin> findSearch(Map whereMap) {
		Specification<Admin> specification = createSpecification(whereMap);
		return adminDao.findAll(specification);
	}

	public List<Admin> findAll() {
		return adminDao.findAll();
	}


	public Admin findById(String id) {
		return adminDao.findById(id).get();
	}

}
